package com.yingeo.ai.sdk.demo.utils;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yingeo.ai.sdk.demo.R;

public abstract class SearchKeyBoardViewHandler implements View.OnClickListener, View.OnLongClickListener {

    private TextView mTextView;
    private LinearLayout rootView;

    public SearchKeyBoardViewHandler(ViewGroup parentView) {
        View view = LayoutInflater.from(parentView.getContext())
                .inflate(R.layout.layout_search_keyboard_view, parentView);
        rootView = view.findViewById(R.id.ll_key_root);
        initKeyboard();
    }

    private void initKeyboard() {
        int count = rootView.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = rootView.getChildAt(i);
            if (child instanceof ViewGroup) {
                ViewGroup layout = (ViewGroup) child;
                if (layout instanceof RelativeLayout) {
                    layout = (ViewGroup) layout.getChildAt(0);
                }
                int childCount = layout.getChildCount();
                if (childCount == 0) continue;
                for (int n = 0; n < childCount; n++) {
                    View subView = layout.getChildAt(n);
                    subView.setOnClickListener(this);
                    int subViewId = subView.getId();
                    if (subViewId == R.id.rl_del) {
                        subView.setOnLongClickListener(this);
                    }
                }
            }
        }
    }

    public void setInputTextView(TextView textView) {
        this.mTextView = textView;
    }

    @Override
    public void onClick(View v) {
        if (v instanceof TextView) {
            inputText(((TextView) v).getText().toString());
        } else if (v.getId() == R.id.rl_del) {
            dealWithDel(false);
        }
    }

    @Override
    public boolean onLongClick(View view) {
        if (view.getId() == R.id.rl_del) {
            dealWithDel(true);
        }
        return true;
    }

    private void dealWithDel(boolean isLongClick) {
        if (mTextView != null) {
            String originalStr = mTextView.getText().toString().trim();
            if (TextUtils.isEmpty(originalStr)) return;
            if (isLongClick) {
                mTextView.setText("");
            } else {
                int length = originalStr.length();
                String newStr = originalStr.substring(0, length - 1);
                mTextView.setText(newStr);
            }
        }
        onTextChanged();
    }

    private void inputText(String value) {
        if (mTextView != null) {
            if (TextUtils.isEmpty(value)) return;
            String mOriginalValue = mTextView.getText().toString().trim();
            String newValue = mOriginalValue + value;
            mTextView.setText(newValue);
        }
        onTextChanged();
    }

    protected abstract void onTextChanged();
}
